package soulagicom.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import soulagicom.model.Avenger;



@Repository
@Transactional
public class AvengerRepository {

	@PersistenceContext
	EntityManager em;


	
	public List<Avenger> findByLastName(String lastName) {
		try {
			return (List<Avenger>) em.createQuery("select a from Avenger a where a.lastName = :lastName")
					.setParameter("lastName", lastName)
					.getResultList();	
		} catch (NoResultException e) {  
			return null;
		}
	
	}

	public List<Avenger> findByFirstName(String firstName) {
		try {
			return (List<Avenger>) em.createQuery("select a from Avenger a where a.firstName = :firstName")
					.setParameter("firstName", firstName)
					.getResultList();	
		} catch (NoResultException e) {  
			return null;
		}
	}

	public Avenger findById(Long id) {
		return (Avenger) em.createQuery("select a from Avenger a where a.id = :id")
				.setParameter("id", id)
				.getSingleResult();
	}	

	public Avenger findByPhone(String phoneNumber) {
		try {
			return (Avenger) em.createQuery("select a from Avenger a where a.phoneNumber = :phoneNumber")
					.setParameter("phoneNumber", phoneNumber)
					.getSingleResult();
		} catch (NoResultException e) {  
			return null;
		}
	}

	public Avenger findByEmailAddress(String emailAddress) {
		try {
			return (Avenger) em.createQuery("select a from Avenger a where a.emailAddress = :emailAddress")
					.setParameter("emailAddress", emailAddress)
					.getSingleResult();
		} catch (NoResultException e) {  
			return null;
		}

	}

	public void persistAvenger(Avenger a) {
		em.persist(a);
	}
	public void deleteAvenger(Avenger a) {
		em.remove(a);


	}

	public List<Avenger> findAllAvengers() {
		return (List<Avenger>) em.createQuery("select a from Avenger a")
				.getResultList();		
	}

	public void merge(Avenger editedAvenger) {
		em.merge(editedAvenger);
	}



}
